package com.nf.health.app.adapter;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.customview.SwipeView;
import com.nf.health.app.customview.SwipeView.OnSwipeStatusChangeListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/** 
 * @ClassName: OldCallAdapter 
 * @Description: 老人机快速拨号快捷键
 * @author Ian 
 * @date 2015-11-24 上午9:55:26 
 *  
 */
public class OldCallAdapter extends BaseAdapter {

	private Context context;
	private List<Map<String,String>> doctors;
	private ArrayList<SwipeView> unClosedSwipeView = new ArrayList<SwipeView>();

	public OldCallAdapter(Context context, List<Map<String, String>> doctors) {
		super();
		this.context = context;
		this.doctors = doctors;
	}

	@Override
	public int getCount() {
		return doctors.size();
	}

	@Override
	public Object getItem(int position) {
		return doctors.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		holder = new ViewHolder();
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.oldcall_item, null);
			holder = new ViewHolder();
			holder.name = (TextView) convertView.findViewById(R.id.oldcall_name);
			holder.num = (TextView) convertView.findViewById(R.id.oldcall_no);
			holder.phonenum = (TextView) convertView.findViewById(R.id.oldcall_phonenum);
			holder.all_lin = (LinearLayout) convertView.findViewById(R.id.all_lin);
			SwipeView swipeView  = (SwipeView) convertView.findViewById(R.id.swipeView);
			View delete  = convertView.findViewById(R.id.delete);
			delete.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if(listener != null)
						listener.onClick(position, "");				
				}
			});
			swipeView.setOnSwipeStatusChangeListener(new OnSwipeStatusChangeListener() {

				@Override
				public void onSwiping(SwipeView swipingSwipeView) {
					if (!unClosedSwipeView.contains(swipingSwipeView)) {
						closeAllOpenedSwipeView();
					}
					unClosedSwipeView.add(swipingSwipeView);
				}

				@Override
				public void onOpen(SwipeView openedSwipeView) {
					for (int i = 0; i < unClosedSwipeView.size(); i++) {
						if (unClosedSwipeView.get(i) != openedSwipeView) {
							unClosedSwipeView.get(i).close();
						}
					}
					if (!unClosedSwipeView.contains(openedSwipeView)) {
						unClosedSwipeView.add(openedSwipeView);
					}
				}

				@Override
				public void onClose(SwipeView closedSwipeView) {
					unClosedSwipeView.remove(closedSwipeView);
				}
			});

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.name.setText(doctors.get(position).get("\"name\"").replace("\"",""));
		holder.num.setText(doctors.get(position).get("\"no\"").replace("\"",""));
		holder.phonenum.setText(doctors.get(position).get("\"num\"").replace("\"",""));

		return convertView;
	}
	static class ViewHolder {
		TextView name,num,phonenum;
		LinearLayout bottom_wrapper,all_lin;
	}
	private OnMyClickListener listener;
	public void setOnMyClickListener(OnMyClickListener listener){
		this.listener = listener;
	}

	private void closeAllOpenedSwipeView() {
		for (int i = 0; i < unClosedSwipeView.size(); i++) {
			if (unClosedSwipeView.get(i).getSwipeStatus() != SwipeView.SwipeStatus.Close) {
				unClosedSwipeView.get(i).close();
			}
		}
	}

}
